/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.campusinfoeducation.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.campusinfo.modules.campusinfo.entity.CampusinfoSchool;
import com.lczyfz.campusinfo.modules.campusinfo.service.CampusinfoSchoolService;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoCollege;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoCollegeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.campusinfoeducation.entity.CampusinfoMajor;
import com.lczyfz.campusinfo.modules.campusinfoeducation.service.CampusinfoMajorService;

import java.util.List;

/**
 * 专业管理Controller
 *
 * @author yt
 * @version 2017-10-18
 */
@Controller
@RequestMapping(value = "${adminPath}/campusinfoeducation/campusinfoMajor")
public class CampusinfoMajorController extends BaseController {

    @Autowired
    private CampusinfoMajorService campusinfoMajorService;

    @Autowired
    private CampusinfoSchoolService campusinfoSchoolService;

    @Autowired
    private CampusinfoCollegeService campusinfoCollegeService;

    @ModelAttribute
    public CampusinfoMajor get(@RequestParam(required = false) String id) {
        CampusinfoMajor entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = campusinfoMajorService.get(id);
        }
        if (entity == null) {
            entity = new CampusinfoMajor();
        }
        return entity;
    }

    @RequiresPermissions("campusinfoeducation:campusinfoMajor:view")
    @RequestMapping(value = {"list", ""})
    public String list(CampusinfoMajor campusinfoMajor, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<CampusinfoMajor> page = campusinfoMajorService.findPage(new Page<CampusinfoMajor>(request, response), campusinfoMajor);
        List<CampusinfoMajor> campusinfoMajors = campusinfoMajorService.findList(campusinfoMajor);
        model.addAttribute("campusinfoMajors", campusinfoMajors);
        model.addAttribute("page", page);
        return "modules/campusinfoeducation/campusinfoMajorList";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoMajor:view")
    @RequestMapping(value = "form")
    public String form(CampusinfoMajor campusinfoMajor, Model model) {
        CampusinfoSchool campusinfoSchool = new CampusinfoSchool();
        CampusinfoCollege campusinfoCollege = new CampusinfoCollege();
        List<CampusinfoSchool> campusinfoSchools = campusinfoSchoolService.findList(campusinfoSchool);
        List<CampusinfoCollege> campusinfoColleges = campusinfoCollegeService.findList(campusinfoCollege);
        model.addAttribute("campusinfoSchools", campusinfoSchools);
        model.addAttribute("campusinfoColleges", campusinfoColleges);
        model.addAttribute("campusinfoMajor", campusinfoMajor);
        return "modules/campusinfoeducation/campusinfoMajorForm";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoMajor:edit")
    @RequestMapping(value = "save")
    public String save(CampusinfoMajor campusinfoMajor, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, campusinfoMajor)) {
            return form(campusinfoMajor, model);
        }
        campusinfoMajorService.save(campusinfoMajor);
        addMessage(redirectAttributes, "保存专业信息成功");
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoMajor/?repage";
    }

    @RequiresPermissions("campusinfoeducation:campusinfoMajor:edit")
    @RequestMapping(value = "delete")
    public String delete(CampusinfoMajor campusinfoMajor, RedirectAttributes redirectAttributes) {
        campusinfoMajorService.delete(campusinfoMajor);
        addMessage(redirectAttributes, "删除专业信息成功");
        return "redirect:" + Global.getAdminPath() + "/campusinfoeducation/campusinfoMajor/?repage";
    }

}